<?php

/* ************************************************************************************

File:					admin.estateagent.php
EstateAgent Improved - a Joomla Component
Copyright:			2004 - 2006 by Darko Selesi

Year: 2007
Improved by: Manos Orfanoudakis - "vosk"
Homepage: 		http://www.eaimproved.eu
Version: 			1.3.x                     
License:			GNU/GPL http://www.gnu.org/copyleft/gpl.html
*************************************************************************** */


// ensure this file is being included by a parent file
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

ini_set('display_errors','1');
error_reporting(2047);
require_once( $mosConfig_absolute_path."/administrator/components/com_estateagent/class.estateagent.php");
require_once("classes/manage.estateagent.php");
require_once( $mainframe->getPath( 'admin_html' ) );


$option 		= mosGetParam( $_REQUEST, 'option', 'com_estateagent' );
$act 			= mosGetParam( $_REQUEST, 'act', 'manageObj' );
$task 		= mosGetParam( $_REQUEST, 'task', '' );
$id 			= mosGetParam( $_REQUEST, 'id', 0 );
$cid     		= mosGetParam( $_REQUEST, 'cid', array(0) );
$limitstart	= mosGetParam( $_REQUEST, 'limitstart', 0);
$limit			= mosGetParam( $_REQUEST, 'limit', 50);


if (!is_array( $cid )) {
      $cid = array(0);
}
if(!$task&&$act!="gallery")
	backendMenu();
switch ($act) {
	case "gallery":
		$ino=3;//Use index3.php for gallery things.
		require( $mosConfig_absolute_path."/administrator/components/com_estateagent/includes/gallery/admin.gallery.php");
		break;
	case "manageObj":
        manageObj($option, $act, $task, $id, $cid, $limitstart, $limit);
        break;
    case "manageCat":
        manageCat($option, $act, $task, $id, $cid, $limitstart, $limit);
		break;    
    case "manageAgts":  // client (agents) administration
    	// not implemented yet
	   	break;
    case "showStc": // statistic - functionality
		// not implemented yet
		break;	
    case "configEA":	// configuration functionality
        configEA($option, $task);
		break;
	case "galleryRepair":
		galleryRepair($option, $task);
		break;
 	case "EAinfo":
        HTML_estateagent::showEAinfo();
    	break;
}

// backend administration menu
function backendMenu() {
	global $mosConfig_live_site;
?>
	  <table cellpadding="4" cellspacing="0" border="0" width="100%">
    <tr  height="100px"> 
    <td  width="15%" style="text-align:left" ><a href= "http://www.eaimproved.eu" target= "_blank " style= "filter: alpha(opacity=7);" onmouseover= "if( this.filters) { this.filters.alpha.opacity=100; }" onmouseout="if( this.filters) { this.filters.alpha.opacity=70; }"><img style= "-moz-opacity: 0.7;" onmouseover= "this.style.MozOpacity=1.0;" onmouseout= "this.style.MozOpacity = 0.7;" src= " <?php echo  $mosConfig_live_site ;?>/components/com_estateagent/pictures/gui/ealogo.png" alt= "visit our web-pages" border="0"/></a>
    </td>
    <td align="right" >
    <style>
	table.backmenu {
        background-color: #F7F8F9;
        border: solid 1px #d5d5d5;
		align:center;
		text-align:center;
		
		
       width: 480px;
       padding: 10px 10px 10px 10px;
        border-collapse: collapse;
		
    }
    table.backmenu tr {
        background-color: #eeeeee;
		align:center;
    }
    table.backmenu td {
        padding: 3px;
        align: center;
        border: 1px;
		width:120px;
        border-style: solid;
        border-bottom-color: #D6D6D6;
        border-right-color: #D6D6D6;
        border-left-color: #D6D6D6;
        border-top-color: #D6D6D6;
    }
	.backmenu td:hover
    {
        color : #C64934;
		cursor: pointer;
		background-color: #f1e8e6;
			/*padding: 3px 5px 1px 5px;*/
		/*background-color: #B5CDE8;*/
        /*border: 3px solid #30559C;*/
    }
	</style>
    <table class= "backmenu" >
    	<tr  >
 
            
            <td ><a href= "index2.php?option=com_estateagent&act=manageObj" title="<?php echo _EAB_MENU_LINK1; ?>"><div><img src="<?php echo $mosConfig_live_site; ?>/components/com_estateagent/pictures/gui/obj.gif" alt="" border = "0"/> <br />
<?php echo _EAB_MENU_LINK1; ?> </div></a> </td>
			<td ><a  href= "index2.php?option=com_estateagent&act=manageCat" title="<?php echo _EAB_MENU_LINK2; ?>"><div><img src="<?php echo $mosConfig_live_site; ?>/components/com_estateagent/pictures/gui/category.gif" alt="" border = "0"/> <br /><?php echo _EAB_MENU_LINK2; ?> </div></a></td>
			
			<td ><a  href= "index2.php?option=com_estateagent&act=configEA" title="<?php echo _EAB_MENU_LINK4; ?>"><div><img src="<?php echo $mosConfig_live_site; ?>/components/com_estateagent/pictures/gui/config.gif" alt="" border = "0"/> <br /><?php echo _EAB_MENU_LINK4; ?></div> </a></td>
			<td ><a  href= "index2.php?option=com_estateagent&act=EAinfo" title="<?php echo _EAB_MENU_LINK5; ?>"><div><img src="<?php echo $mosConfig_live_site; ?>/components/com_estateagent/pictures/gui/info.gif" alt="" border = "0"/><br /><?php echo _EAB_MENU_LINK5; ?></div> </a></td>
			

		</tr>
        
     </table>
     </td>
    </tr>
    </table>
<?php }

function backendFooter(){
	global $ea_version,$eaconf;
	?>
	<div style="text-align:center;"><a href="http://www.eaimproved.eu" target="_blank\">EA Improved v. <?php echo  $eaconf->ea_version ; ?> </a><br /><span style="font-size:9px"> &copy;  <?php echo  mosCurrentDate( '%Y' ); ?>  by Manos Orfanoudakis</span></div><br />
 <?php	
}

function manageObj($option, $act, $task, $id, $cid, $limitstart, $limit) {
	global $database, $my, $mosConfig_live_site, $mosConfig_absolute_path, $mosConfig_offset, $eaconf;
	
	switch ($task) {
		
		case 'new':
    	case 'edit':
			   				
   			$obj = new EAObject( $database );
   				
			if($task == "edit") {
				if($id == 0) $obj->load($cid[0]);
				else $obj->load($id);
				$action = _EAB_OBJ_EDIT;
			}
     		else{   		
				$action = _EAB_OBJ_NEW;
        	}
			if($obj->isCheckedOut($my->id)){
				mosErrorAlert( "The object [ ".$obj->obj_id." ] is currently being edited by another person.");			
			}
			$obj->checkout( $my->id );	
			$t[] = mosHTML::makeOption('0',_EAB_OBJ_TYPE1);
			$t[] = mosHTML::makeOption('1',_EAB_OBJ_TYPE2);
			$t[] = mosHTML::makeOption('2',_EAB_OBJ_TYPE3);
			$t[] = mosHTML::makeOption('3',_EAB_OBJ_TYPE4);
			$t[] = mosHTML::makeOption('4',_EAB_OBJ_TYPE5);
			$lists['type'] = mosHTML::selectList($t,'type','id="type" class="inputbox" size="1" onChange="javascript:changeGUI(document.getElementById(\'type\').selectedIndex);" ', 'value', 'text', $obj->type );
				
			$obj->lift					= mosEAHTML::checkbox('lift', 'class="inputbox"', '1', '', 0, $obj->lift );
			$obj->handicap			= mosEAHTML::checkbox('handicap', 'class="inputbox"', '1', '', 0, $obj->handicap );
   			$obj->toheat_ufloor		= mosEAHTML::checkbox( 'toheat_ufloor', 'class="inputbox"', '1', '', 0, $obj->toheat_ufloor );
  			$obj->fireside				= mosEAHTML::checkbox( 'fireside', 'class="inputbox"', '1', '', 0, $obj->fireside );
			$obj->aircondition		= mosEAHTML::checkbox( 'aircondition', 'class="inputbox"', '1', '', 0, $obj->aircondition );
			$obj->tv						= mosEAHTML::checkbox( 'tv', 'class="inputbox"', '1', '', 0, $obj->tv );
  			$obj->dsl					= mosEAHTML::checkbox( 'dsl', 'class="inputbox"', '1', '', 0, $obj->dsl );
   			$obj->balcony			= mosEAHTML::checkbox( 'balcony', 'class="inputbox"', '1', '', 0, $obj->balcony );   			
   			$obj->terrace				= mosEAHTML::checkbox( 'terrace', 'class="inputbox"', '1', '', 0, $obj->terrace );
   			$obj->garden				= mosEAHTML::checkbox( 'garden', 'class="inputbox"', '1', '', 0, $obj->garden );
			$obj->swpool				= mosEAHTML::checkbox( 'swpool', 'class="inputbox"', '1', '', 0, $obj->swpool );
   			$obj->parkarea			= mosEAHTML::checkbox( 'parkarea', 'class="inputbox"', '1', '', 0, $obj->parkarea );   			
   			$obj->garage				= mosEAHTML::checkbox( 'garage', 'class="inputbox"', '1', '', 0, $obj->garage );   			
   			$obj->hot					= mosEAHTML::checkbox( 'hot', 'class="inputbox"', '1', '', 0, $obj->hot );   			
   			 
			// get object images and generate gallery HTML-code
			$gl = "";
			if($task=='edit'){
				$g = $obj->loadGallery($obj->id,1); 
				if(count($g) > 0) { 
					$c = 2;
					foreach($g as $i) {
						if($c%2 == 0) $gl .= "<tr>";   						
						$i->path = ($i->path == "")? $eaconf->ea_picpath : $i->path;
						$gl .= "<td style=\"width:50%; text-align:center\"><img src=\"".$mosConfig_live_site.$i->path.$i->fname.$i->type."\" width=\"250\" border=\"0\" /></td>";  					
						if($c%2 != 0) $gl .= "</tr>";
						$c++;
					}
					if(count($g)%2 != 0) $gl .= "<td>&nbsp;</td></tr>";
				}	
				else {
					$gl = "<tr><td>" . _EAB_OBJ_NOIMG . "</td></tr>";
				}
			}
			else
			{
				$gl = "<tr><td>" . _EAB_OBJ_NOSAVE . "</td></tr>";
			}   
   				  			
   			$lists['countries'] = mosHTML::selectList( $obj->country_select_list(), 'country', 'class="inputbox" size="1"', 'value', 'text', $obj->country );
   			
 	        $lists['access'] = mosAdminMenus::Access( $obj );  				
			
			$active = ( intval( $obj->created_by ) ? intval( $obj->created_by ) : $my->id );
 		    $lists['created_by'] = mosAdminMenus::UserSelect( 'created_by', $active,0,NULL,'name',0 );
 		    
			$obj->created = ($obj->created == "")? date( "Y-m-d" ) : $obj->created;
  			$obj->publish_up = ($obj->publish_up == "")? date( "Y-m-d" ) : $obj->publish_up;;
   			if($task == "new" || trim( $obj->publish_down ) == '0000-00-00') {
 	              $obj->publish_down = _EAB_OBJ_PUBNEVER;
 			}
 			$obj->published = mosEAHTML::checkbox( 'published', 'class="inputbox"', '1', '', 0, $obj->published ); 
   			   			  			
   			$params['measure'] 	= $eaconf->ea_measure;
   			$params['currency'] 	= $eaconf->ea_currency;
   			$params['picpath']		= $eaconf->ea_picpath;
   			   			   			   			
   			HTML_estateagent::editObj($option,$act,$action,$obj,$gl,$lists,$params);
   			   			   			  	
		break;
		
		case 'resetC':

			$obj = new EAObject($database);
			$obj->load($id);
			
			$obj->hits = 0;
			$obj->store();
			  
			$msg = _EAB_OBJ_RESETCNT_SUCCESS;
			mosRedirect( 'index2.php?option=com_estateagent&act=manageObj&task=edit&hidemainmenu=1&id='. $id, $msg );

		break;

		case 'unpublish':
        case 'publish':
            
			if (count( $cid ) < 1) {
           		$action = $publish ? 'publish' : 'unpublish';
           		echo "<script> alert('Select a object to $task'); window.history.go(-1);</script>\n";
           		exit;
       		}
   
       		$cids = implode( ',', $cid );
   
   			$publish = ($task == "publish")? "1" : "0";
			
  			$database->setQuery( "UPDATE #__estateagent SET published=$publish WHERE id IN ( $cids )" );
  			$database->query();
  			
  			if(!$database->getErrorNum()) $msg = _EAB_OBJ_SCSS;
			else $msg = _EAB_OBJ_ERROR;
			
			mosRedirect( "index2.php?option=$option&act=manageObj&limitstart=$limitstart&limit=$limit", $msg );
			
 		break;
		//XXX: Extra functions, edit by VOSK     
        case 'clone':
			if (count( $cid ) < 1) {

           		echo "<script> alert('Select a object to $task'); window.history.go(-1);</script>\n";
           		exit;
       		}
			foreach($cid as $id){
				$obj = new EAObject($database);
				$obj->load($id);
				$obj->id = "";
				$obj->obj_id = trim( $obj->obj_id)."-cloned";
				$obj->hits=0;
				$obj->published=0;
				$obj->store();
			}
			if(!$database->getErrorNum()) $msg = _EAB_OBJ_SCSS;
			else $msg = _EAB_OBJ_ERROR;
			mosRedirect( "index2.php?option=$option&act=manageObj&limitstart=$limitstart&limit=$limit",$msg );
			break;
		case 'translate':
			mosRedirect('index2.php?option=com_joomfish&act=translate&catid=estateagent');
			break;
		//XXX: Extra functions, edit by VOSK
        case 'save':
		case "apply":
    		$obj = new EAObject( $database );
    		      		
       		if (!$obj->bind( $_POST )) {
           		echo "<script> alert('".$obj->getError()."'); window.history.go(-1); </script>\n";
           		exit();
      		}
			$obj->id 					= mosGetParam($_POST,'id','');
   			$obj->obj_id				= trim( $obj->obj_id);
  			$obj->description 		= str_replace( '<br>', '<br />', $obj->description );
  			$obj->location			= str_replace( '<br>', '<br />', $obj->location );
  			$obj->condition 			= str_replace( '<br>', '<br />', $obj->condition );
  			$obj->other  				= str_replace( '<br>', '<br />', $obj->other );
			$obj->created				= $obj->created? $obj->created : date( "Y-m-d" );//TODO
  			$obj->created_by    	= $obj->created_by ? $obj->created_by : $my->id;
  													
 			if (!$obj->check()) {
           		echo "<script> alert('".$obj->getError()."'); window.history.go(-1); </script>\n";
           		exit();
       		}
       		if (!$obj->store()) {
           		echo "<script> alert('".$obj->getError()."'); window.history.go(-1); </script>\n";
           		exit();
       		}
			$obj->checkin();	
			$msg=_EAB_OBJ_SCSS;
			if($task=="apply")
					mosRedirect("index2.php?option=$option&act=manageObj&hidemainmenu=1&task=edit&id=". $obj->id ,$msg);
			mosRedirect( "index2.php?option=$option&act=manageObj&limitstart=$limitstart&limit=$limit",$msg );
    		  
	  	break;
        
        case 'remove':			
			
			$msg = _EAB_OBJ_DELSCSS;
			
			$total = count( $cid );
 			if ( $total < 1) {
           		echo "<script> alert('"._EAB_OBJ_ERROR16."'); window.history.go(-1);</script>\n";
           		exit;
       		}
			if(count($cid) < 1){
				$cid[0] = $id;
			}			
			
			foreach($cid as $o){							
				$obj = new EAObject($database);
				//delete object images
				//$images = $obj->getObjectImages($o);							
				//foreach($images as $i) {								
					//$img = new EAImage($database);
					//$img->load($i);
					//$img->delete();
				//}
				if(!$obj->delete($o)) $msg = _EAB_OBJ_DELERROR;
			}
				
			mosRedirect( "index2.php?option=com_estateagent&act=manageObj&limitstart=$limitstart&limit=$limit", $msg );
			
 		break;
		case 'cancel':
			$obj = new EAObject( $database );
    		      		
       		if (!$obj->bind( $_POST )) {
           		echo "<script> alert('".$obj->getError()."'); window.history.go(-1); </script>\n";
           		exit();
      		}
			$obj->checkin();
			mosRedirect( "index2.php?option=com_estateagent&act=manageObj&limitstart=$limitstart&limit=$limit" );
			break;
		case 'accesspublic':
 		case 'accessregistered':
        case 'accessspecial':
                   
      		$row = new EAObject( $database );
      		$row->load( $cid[0] );
      		switch($task){
			
				case 'accesspublic':
					$row->access = 0;
				break;
  			
				case 'accessregistered':
					$row->access = 1;
				break;
				
				case 'accessspecial':
					$row->access = 2;
				break;
			}

      		if ( !$row->check() ) {
          		return $row->getError();
      		}
      		if ( !$row->store() ) {
          		return $row->getError();
      		}
    
      		mosRedirect( "index2.php?option=com_estateagent&act=manageObj&limitstart=$limitstart&limit=$limit" );
  
		break;

		default:
           	
			
			$sort= EAadmin::getOrderingQuery();
			$where=  EAadmin::getFilteringQuery();
			
			
    		    		
			$query = "SELECT count(o.id)"
						. " FROM (#__estateagent AS o, #__estateagent_categories AS c)"
						. " LEFT JOIN #__groups AS g ON g.id = o.access"	
						. " WHERE c.id = o.cat $where";
			$database->setQuery($query);
			$total = $database->loadResult();    		

    		require_once("includes/pageNavigation.php");
  			$pageNav = new mosPageNav( $total, $limitstart, $limit );
						
			$query = "SELECT IF( o.rent > o.price, o.rent, o.price ) AS 'value', o.id, o.obj_id, o.cat, o.title, o.country, o.locstate, o.town, o.district, o.hits, o.publish_up, o.publish_down, o.published, o.hot, o.access, c.name AS catname, g.name AS groupname , o.checked_out , o.checked_out_time, u.name AS editor"
						. " FROM (#__estateagent AS o, #__estateagent_categories AS c ) "
						. " LEFT JOIN #__groups AS g ON g.id = o.access LEFT JOIN #__users AS u ON u.id = o.checked_out"	
						. " WHERE c.id = o.cat $where $sort LIMIT $limitstart,$limit";
			$database->setQuery($query);
			$objects = $database->loadObjectList();

			echo $database->getErrorMsg();			       		
       					
       		$query = "SELECT count(id) FROM #__estateagent_categories WHERE parent = 0";
			$database->setQuery($query);
			$cats = $database->loadResult();		
        	
        	if($cats > 0) {  				

    			HTML_estateagent::showObjOverview($option,$act,$objects,$pageNav);
        	}
        	else{
        		$msg = _EAB_CAT_CREATECAT;   					
   				mosRedirect( "index2.php?option=$option&act=manageCat", $msg );
        	}
        		
    	break;
	}
}
// ---------------------------------------------------------
// category functions --------------------------------
// ---------------------------------------------------------
function manageCat($option, $act, $task, $id, $cid, $limitstart, $limit){
	global $database,$mosConfig_absolute_path;
	$recurse =mosGetParam( $_REQUEST, 'catrecurse', 0 );
	if($recurse==1){
				function parentlistR(&$array,$parent){
				global $database;
				$query = "SELECT id  FROM #__estateagent_categories WHERE  parent='$parent'";
        		$database->setQuery($query);      
				$cats=$database->loadResultArray();
				foreach($cats as $cat){
					$array[]  = $cat;
					parentlistR($array,$cat);
				}		
				 
			}
			$oldcid= $cid;
			foreach($oldcid as $id){
				parentlistR($cid,$id);
			}
	
	}
	switch($task) {
		
		case "new":
		case "edit":	
						
			$lists = array();
					
 			$cat = new EACategory($database);	   		
	   		
			if($id == 0 && $cid[0] == 0){
				// initialise new record         	
          		$cat->parent 	= 0;
          		$cat->ordering  	= 0;
          		$cat->published   	= 1; 	   
			}
			elseif($id == 0){
 				$cat->load($cid[0]);
				$id= $cid[0];
			}
			else {
				$cat->load($id);
				$cid[0]=$id;
			}
    		
			$cat->publish_up = ($cat->publish_up == "")? date( "Y-m-d" ) : $cat->publish_up;
   			if($task == "new" || trim( $cat->publish_down ) == '0000-00-00') {
 	              $cat->publish_down = _EAB_OBJ_PUBNEVER;
 			}
			
			$query = "SELECT count(*) FROM #__estateagent_categories";
			$database->setQuery($query);
			$total = $database->loadResult();
    		
    	    // build the html select list for ordering
 		    $query = "SELECT ordering AS value, name AS text"
       					. "\n FROM #__estateagent_categories"
       					. "\n WHERE parent=$cat->parent"
       					. "\n ORDER BY ordering";
       		$val = ($total > 0)? 1 : 0;
			$lists['ordering'] = mosAdminMenus::SpecificOrdering( $cat, $val, $query, 0 );
    	    
    	    // build the html select list for the group access
 	        $lists['access'] = mosAdminMenus::Access( $cat ); 
			$rows=array(); 				
			function parentCatListR(&$options,$parent,&$id,$indend){
				global $database;
				$query = "SELECT id AS value, name AS text FROM #__estateagent_categories WHERE  parent='$parent' AND id != '$id'";
        		$database->setQuery($query);      
				$cats=$database->loadObjectList();
				foreach($cats as $cat){
					$options[]  = mosHTML::makeOption($cat->value,$indend."->".$cat->text);
					parentCatListR($options,$cat->value,$id,$indend."&nbsp;&nbsp;&nbsp;");
				}		
				 
			}
			 $rows[] = mosHTML::makeOption( 0, _EAB_CAT_OVW7 );	        	
        	parentCatListR($rows,0,$cid[0],"");
			
	        //if($total > 0) $rows = array_merge( $rows, $database->loadObjectList() );       	
        	$lists['parent'] = mosHTML::selectList($rows,'parent','size="1" class="inputbox" style="width:160px"','value','text', $cat->parent);
			  
			$lists['publish']= mosHTML::yesnoRadioList( 'published', 'class="inputbox"', $cat->published );
			
  			HTML_estateagent::editCat($option, $act, $cat, $lists);
		
		break;
				
		case "save":
		case "apply":
			$msg=_EAB_CAT_SCSS;				  
			$row = new EACategory( $database );
       		
       		if (!$row->bind( $_POST )) {
           		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>";
           		exit();
      		}   
  			$row->description 	= str_replace( '<br>', '<br />', $row->description );
  														
 			if (!$row->check()) {
           		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>";
           		exit();
       		}
       		if (!$row->store()) {
           		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>";
           		exit();
       		}
       		
       		if($row->parent != 0){
       			$row->updateOrder( "parent=$row->parent" );
				$c = new EACategory($database);
				$c->updateOrder( "parent=0" );	
       		}
       		else{
   				$row->updateOrder( "parent=0" );
			}
       		if($task=="apply")
					mosRedirect("index2.php?option=$option&act=manageCat&hidemainmenu=1&task=edit&id=". $row->id ,$msg);
					
			mosRedirect( "index2.php?option=$option&act=manageCat&limitstart=$limitstart&limit=$limit",$msg );
			
		break;
 		
 		case "remove":
			HTML_estateagent::catDelete($option, $act,implode(";",$cid));
			break;
		case "performremove":		 
			$cid=explode(";",mosGetParam( $_REQUEST, 'cids', array() ));
			$subcats=mosGetParam( $_REQUEST, 'subcats', 0);
			//if(count($cid) < 1) {
				//$cid[0] = $id;
			//}
			$msg=_EAB_CAT_DELSCSS;
			function deleteCat($id,&$msg){
				global $database;
				$cat = new EACategory($database);
				$cat->load($id);
				//First Delete Cat objects...
				
				$objects = $cat->getCatEntries();
				$obj = new EAObject($database);												
				foreach($objects as $o){							
					
					if(!$obj->delete($o)){
						$msg=_EAB_CAT_DELERROR;
						return;
					}
					
				}
				//Then recurse to subcats
				$scats = $cat->getSubcategories();
				foreach($scats as $sc){
					deleteCat($sc->id,$msg);
				}
					

				//delete self
				$p = $cat->parent;
				if(!$cat->delete()){
						$msg=_EAB_CAT_DELERROR;
						return;
					}
				$cat->updateOrder( "parent='".$p."'" );
			}
			$cat = new EACategory($database);			
			foreach($cid as $c){
				$cat->load($c);
				if(!$subcats){
					$p=$cat->parent;
					$q= "UPDATE #__estateagent_categories SET parent=$p WHERE parent=$cat->id";
					//echo $q;
					$database->setQuery($q);
			 	    $database->query();
					if ($database->getErrorNum()) {
						$msg=_EAB_CAT_DELERROR;
						break;
					}
		
				}
				//Delete recursively, if !$subcats, no recursion occurs.
				deleteCat($cat->id,$msg);				
			}
			//exit();
			mosRedirect("index2.php?option=$option&act=manageCat&limitstart=$limitstart&limit=$limit",$msg );
			
 		break;
 		
 		case "publish":
 		case "unpublish":
 			
			if (count( $cid ) < 1) {
           		//$action = $publish ? 'publish' : 'unpublish';
           		echo "<script> alert('"._EAB_OBJ_ERROR17."'); window.history.go(-1);</script>\n";
           		exit;
       		}
			
			$cids = implode( ',', $cid );
   			
			$status = ($task == "publish")? 1 : 0;
						
   			$database->setQuery( "UPDATE #__estateagent_categories SET published=$status WHERE id IN ($cids)" );
   			$database->query();
			$error = $database->getErrorMsg();
			if(!empty($error)){
				echo "<script> alert('". $error ."'); window.history.go(-1); </script>\n";
				exit();
			}
			else{
				mosRedirect( "index2.php?option=$option&act=manageCat&limitstart=$limitstart&limit=$limit" );
			}

 		break;
		
		case "orderup":
		       
       		$row = new EACategory( $database );
      		$row->load( $cid[0] );
			$row->move( -1, "parent = $row->parent" );			
			
			mosRedirect( "index2.php?option=$option&act=manageCat&limitstart=$limitstart&limit=$limit" ); 
		
		break;
		
		case "orderdown":
			$row = new EACategory( $database );
      		$row->load( $cid[0] );
			$row->move( 1, "parent = $row->parent" );
								
			mosRedirect( "index2.php?option=$option&act=manageCat&limitstart=$limitstart&limit=$limit" );
		
		break;
		
		case 'accesspublic':
 		case 'accessregistered':
        case 'accessspecial':
             
			 
			 foreach($cid as $c){
      			$row = new EACategory( $database );
      			$row->load(intval( $c ) );
      			switch($task){
			
					case 'accesspublic':
						$row->access = 0;
					break;
  			
					case 'accessregistered':
						$row->access = 1;
					break;
				
					case 'accessspecial':
						$row->access = 2;
					break;
				}
				if ( !$row->check() ) {
          			return $row->getError();
	      		}
    	  		if ( !$row->store() ) {
        	  		return $row->getError();
      			}
			}

      		
    
      		mosRedirect( "index2.php?option=com_estateagent&act=manageCat&limitstart=$limitstart&limit=$limit" );
  
		break;
		case 'cancel':
			mosRedirect( "index2.php?option=com_estateagent&act=manageCat&limitstart=$limitstart&limit=$limit" );
			break;
		default:
	
			$query = "SELECT count(id) FROM #__estateagent_categories";
			$database->setQuery($query);
			$total = $database->loadResult();	
			
			$query = "SELECT id FROM #__estateagent_categories WHERE published = 0";
			$database->setQuery($query);
			$unpub = $database->loadResultArray();
			if(count($unpub)==1){
				$q="SELECT count(id) FROM  #__estateagent_categories WHERE published=1 AND parent =".$unpub[0];
				$database->setQuery($q);
				$hidden = $database->loadResult();	
			
			}
			elseif(count($unpub)>1){	
				$q="SELECT count(id) FROM  #__estateagent_categories WHERE published=1 AND parent IN (".implode(",",$unpub).")";
				$database->setQuery($q);
				$hidden = $database->loadResult();	
			}
			else 
				$hidden=0;
		
			HTML_estateagent::showCatOverview($option, $act,$total,$hidden);
    		
		break;
	}
}

#########################################################################
# Configuration Functions
#########################################################################

function configEA($option, $task) {
		global $mosConfig_absolute_path,$eaconf;
	switch($task) {
			
 		case "save":
			
    		    		
    		$cfgfile = $mosConfig_absolute_path."/administrator/components/com_estateagent/configuration.php";
  			$permission = is_writable($cfgfile);  			
  			if ($permission == false) @chmod ($cfgfile, 0777);
  			
    		$object = $_POST;
    		$object['ea_offmessage'] 	= addslashes($object['ea_offmessage']);
    		$object['ea_measure'] 		= ($object['ea_measure'] == "m") ? "m&sup2;" : "ft&sup2;";
			$object['ea_imgprtn'] 			=  ($object['ea_imgprtn'])? $object['ea_imgprtn'] : "0";
			$object['ea_tbprtn'] 			=  ($object['ea_tbprtn'])? $object['ea_tbprtn'] : "0";
			$object['ea_map24_tlogo'] 	= (stristr($object['ea_map24_tlogo'],"http://")) ? $object['ea_map24_tlogo'] : "http://" . $object['ea_map24_tlogo'];			
			$object['ea_map24_dimg'] 	= (stristr($object['ea_map24_dimg'],"http://")) ? $object['ea_map24_dimg'] : "http://" . $object['ea_map24_dimg'];
			
		
    		$cfg = "<?php\n";
    		$cfg .= "defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );\n\n";
  			$cfg .= "\$ea_picpath = \"".$object['ea_picpath']."\";\n";
  			$cfg .= "\$ea_offline = \"".$object['ea_offline']."\";\n";
			$cfg .= "\$ea_disperrors = \"".$object['ea_disperrors']."\";\n";
  			$cfg .= "\$ea_offmessage = \"".trim($object['ea_offmessage'])."\";\n";
  			$cfg .= "\$ea_sorting = \"".$object['ea_sorting']."\";\n";
  			$cfg .= "\$ea_perpage = \"".$object['ea_perpage']."\";\n";
  			$cfg .= "\$ea_cat_entries = \"".$object['ea_cat_entries']."\";\n";
  			$cfg .= "\$ea_show_scats = \"".$object['ea_show_scats']."\";\n";
			$cfg .= "\$ea_mcat_fullview = \"".$object['ea_mcat_fullview']."\";\n";
  			//$cfg .= "\$ea_hot_pos = \"".$object['ea_hot_pos']."\";\n";
  			$cfg .= "\$ea_hot_color = \"".$object['ea_hot_color']."\";\n";			
			$cfg .= "\$ea_shortinfo = \"".$object['ea_shortinfo']."\";\n";
			$cfg .= "\$ea_fulllocation = \"".$object['ea_fulllocation']."\";\n";
			$cfg .= "\$ea_mtf = \"".$object['ea_mtf']."\";\n";
			$cfg .= "\$ea_f_srch = \"".$object['ea_f_srch']."\";\n";
  			$cfg .= "\$ea_l_srch = \"".$object['ea_l_srch']."\";\n";
			$cfg .= "\$ea_loc_srchtype = \"".$object['ea_loc_srchtype']."\";\n";
  			$cfg .= "\$ea_d_srch = \"".$object['ea_d_srch']."\";\n";
  			$cfg .= "\$ea_fpstyle = \"".$object['ea_fpstyle']."\";\n";
			$cfg .= "\$ea_hotfp = \"".$object['ea_hotfp']."\";\n";
			$cfg .= "\$ea_hotfp_pos = \"".$object['ea_hotfp_pos']."\";\n";
 			$cfg .= "\$ea_hotfpa = \"".$object['ea_hotfpa']."\";\n";
  			$cfg .= "\$ea_map24 = \"".$object['ea_map24']."\";\n";
  			$cfg .= "\$ea_map24_lid = \"".trim($object['ea_map24_lid'])."\";\n";
  			$cfg .= "\$ea_map24_type = \"".$object['ea_map24_type']."\";\n";
  			$cfg .= "\$ea_map24_dinfo = \"".trim($object['ea_map24_dinfo'])."\";\n";
  			$cfg .= "\$ea_map24_tlogo = \"".trim($object['ea_map24_tlogo'])."\";\n";
			$cfg .= "\$ea_map24_dimg = \"".trim($object['ea_map24_dimg'])."\";\n";
  			$cfg .= "\$ea_map24_dlg = \"".trim($object['ea_map24_dlg'])."\";\n";
    		$cfg .= "\$ea_maxpicsize = \"".$object['ea_maxpicsize']."\";\n";
    		$cfg .= "\$ea_currency = \"".$object['ea_currency']."\";\n";
    		$cfg .= "\$ea_currency_pos = \"".$object['ea_currency_pos']."\";\n";
	   		$cfg .= "\$ea_currency_digits = \"".$object['ea_currency_digits']."\";\n";			
    		$cfg .= "\$ea_nformat = \"".$object['ea_nformat']."\";\n";
    		$cfg .= "\$ea_mtgcalc = \"".$object['ea_mtgcalc']."\";\n";
			$cfg .= "\$ea_mtg_default_interest_percent = \"".$object['ea_mtg_default_interest_percent']."\";\n";
			$cfg .= "\$ea_mtg_default_year_term = \"".$object['ea_mtg_default_year_term']."\";\n";
			$cfg .= "\$ea_mtg_default_down_percent = \"".$object['ea_mtg_default_down_percent']."\";\n";
			$cfg .= "\$ea_mtg_default_show_progress = \"".$object['ea_mtg_default_show_progress']."\";\n";
			$cfg .= "\$ea_measure = \"".$object['ea_measure']."\";\n";
    		$cfg .= "\$ea_maxpics = \"".$object['ea_maxpics']."\";\n";
    		$cfg .= "\$ea_gplib = \"".$object['ea_gplib']."\";\n";
    		$cfg .= "\$ea_imgwidth = \"".$object['ea_imgwidth']."\";\n";
        	$cfg .= "\$ea_imgheight = \"".$object['ea_imgheight']."\";\n";
			$cfg .= "\$ea_imgprtn = \"".$object['ea_imgprtn']."\";\n";
			$cfg .= "\$ea_imgqulty = \"".$object['ea_imgqulty']."\";\n";
			$cfg .= "\$ea_watermark = \"".$object['ea_watermark']."\";\n";
    		$cfg .= "\$ea_tbwidth = \"".$object['ea_tbwidth']."\";\n";
  			$cfg .= "\$ea_tbheight = \"".$object['ea_tbheight']."\";\n";
  			$cfg .= "\$ea_tbprtn = \"".$object['ea_tbprtn']."\";\n";
  			$cfg .= "\$ea_tbqulty = \"".$object['ea_tbqulty']."\";\n";
			$cfg .= "\$ea_allowsubmission = \"".$object['ea_allowsubmission']."\";\n";
			$cfg .= "\$ea_frontaccess = \"".$object['ea_frontaccess']."\";\n";
			$cfg .= "\$ea_frontsuper = \"".$object['ea_frontsuper']."\";\n";
			$cfg .= "\$ea_allowpublish = \"".$object['ea_allowpublish']."\";\n";
			$cfg .= "\$ea_mail = \"".$object['ea_mail']."\";\n";
			$cfg .= "\$ea_maxobjects = \"".$object['ea_maxobjects']."\";\n";
			$cfg .= "\$ea_rss = \"".$object['ea_rss']."\";\n";
			$cfg .= "\$ea_footer = \"".$object['ea_footer']."\";\n";
			$cfg .= "\$ea_template = \"".$object['ea_template']."\";\n";
			$cfg .= "\$ea_print = \"".$object['ea_print']."\";\n";
			$cfg .= "\$ea_captcha = \"".$object['ea_captcha']."\";\n";
			//Google Map
			$cfg .= "\$ea_googlemap_enable = \"".$object['ea_googlemap_enable']."\";\n";
			$cfg .= "\$ea_googlemap_key = \"".$object['ea_googlemap_key']."\";\n";
			$cfg .= "\$ea_googlemap_sitemark = \"".$object['ea_googlemap_sitemark']."\";\n";
  			$cfg .= "?>";

  			if ($fp = fopen("$cfgfile", "w")) {
    			fputs($fp, $cfg, strlen($cfg));
    			fclose ($fp);
    			@chmod ($cfgfile, 0644);
    			$msg = _EAB_CFG_SCSS;
    		}
			else{
				$msg = _EAB_CFG_ERROR1;
			}
			
    		mosRedirect( "index2.php?option=$option&act=configEA", $msg );
    		
 		break;
		
		default:
		
			require($mosConfig_absolute_path."/administrator/components/com_estateagent/configuration.php");
			         
			//=============Templating========================================
			$templates=EA_Template::getAvailableTemplates();
			foreach($templates as $t){
				$tlist[]=mosHTML::makeOption( $t, $t );
			
			}
			 $lists['ea_template'] = mosHTML::selectList( $tlist, 'ea_template', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_template );
			
			//===============================================================	   						
			$yesno[] = mosHTML::makeOption( '0', _EAB_CFG_STS01 );
        	$yesno[] = mosHTML::makeOption( '1', _EAB_CFG_STS02 );
			$lists['ea_offline'] = mosHTML::selectList( $yesno, 'ea_offline', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_offline );
			$lists['ea_captcha'] = mosHTML::selectList( $yesno, 'ea_captcha', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_captcha );
			 $lists['ea_disperrors']=mosHTML::selectList( $yesno, 'ea_disperrors', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_disperrors );
			$lists['ea_offmessage'] = $eaconf->ea_offmessage;
			
			$lists['ea_picpath'] = $eaconf->ea_picpath;
			$lists['picpath_avalaible'] = (@is_dir($mosConfig_absolute_path.$eaconf->ea_picpath))? "<span style='color:green; font-weight:bold'>"._EAB_CFG_STS02."</span>" : "<span style='color:red; font-weight:bold'>"._EAB_CFG_STS01."</span>";
			$lists['picpath_writeable'] = (@is_writeable($mosConfig_absolute_path.$eaconf->ea_picpath))? "<span style='color:green; font-weight:bold'>"._EAB_CFG_STS02."</span>" : "<span style='color:red; font-weight:bold'>"._EAB_CFG_STS01."</span>";
			
			$eaconf->ea_measure = ($eaconf->ea_measure == "m&sup2;") ? "m" : "ft";
            $ms[] = mosHTML::makeOption( 'm', _EAB_CFG_STS03 );
            $ms[] = mosHTML::makeOption( 'ft', _EAB_CFG_STS04 );
            $lists['ea_measure'] = mosHTML::selectList( $ms, 'ea_measure', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_measure );
			
			$lists['ea_currency'] = $eaconf->ea_currency;
			$lists['ea_currency_digits']= $eaconf->ea_currency_digits;
			$nf[] = mosHTML::makeOption( '0', _EAB_CFG_STS152 );
            $nf[] = mosHTML::makeOption( '1', _EAB_CFG_STS153 );
            $lists['ea_nformat'] = mosHTML::selectList( $nf, 'ea_nformat', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_nformat );
						
			$fps[] = mosHTML::makeOption( '0', _EAB_CFG_STS08 );
    		$fps[] = mosHTML::makeOption( '1', _EAB_CFG_STS09 );    		
            $lists['ea_fpstyle'] = mosHTML::selectList( $fps, 'ea_fpstyle', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_fpstyle );
		
			$hot[] = mosHTML::makeOption( '0', _EAB_CFG_HOTOPTION0 );
			$hot[] = mosHTML::makeOption( '1', _EAB_CFG_HOTOPTION1 );
			$hot[] = mosHTML::makeOption( '2', _EAB_CFG_HOTOPTION2 );
			$hot[] = mosHTML::makeOption( '3', _EAB_CFG_HOTOPTION3 );
			$lists['ea_hotfp'] = mosHTML::selectList( $yesno, 'ea_hotfp', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_hotfp );						
        	$lists['ea_hotfpa'] = mosHTML::integerSelectList( 1, 15, 1, 'ea_hotfpa', 'class="inputbox"', $eaconf->ea_hotfpa, $format="" );

			$lists['ea_maxpicsize'] = $eaconf->ea_maxpicsize;
			$lists['ea_maxpics'] =$eaconf->ea_maxpics; 
			$glb[] = mosHTML::makeOption( '0', _EAB_CFG_STS01 );
            $glb[] = mosHTML::makeOption( 'gd1', 'GD' );
        	$glb[] = mosHTML::makeOption( 'gd2', 'GD2' );
        	$lists['ea_gplib'] = mosHTML::selectList( $glb, 'ea_gplib', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_gplib );
			$lists['ea_imgwidth'] = $eaconf->ea_imgwidth;
        	$lists['ea_imgheight'] = $eaconf->ea_imgheight;
			$lists['ea_imgprtn'] = ($eaconf->ea_imgprtn == "1")? " checked" : "";
			$lists['ea_imgqulty'] = $eaconf->ea_imgqulty;			        	
        	$lists['ea_tbwidth'] = $eaconf->ea_tbwidth;
        	$lists['ea_tbheight'] = $eaconf->ea_tbheight;
			$lists['ea_tbprtn'] = ($eaconf->ea_tbprtn == "1")? " checked" : "";
			$lists['ea_tbqulty'] = $eaconf->ea_tbqulty;
			$lists['ea_watermark'] = mosHTML::selectList( $yesno, 'ea_watermark', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_watermark );					
        	
			$lists['ea_cat_entries'] = mosHTML::selectList( $yesno, 'ea_cat_entries', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_cat_entries );
        	$lists['ea_show_scats'] = mosHTML::selectList( $yesno, 'ea_show_scats', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_show_scats );
			$lists['ea_mcat_fullview'] = mosHTML::selectList( $yesno, 'ea_mcat_fullview', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_mcat_fullview );

			$hp[] = mosHTML::makeOption( '0', _EAB_CFG_STS212 );
            $hp[] = mosHTML::makeOption( '1', _EAB_CFG_STS213 );
        	
			$lists['ea_currency_pos'] =mosHTML::selectList( $yesno, 'ea_currency_pos', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_currency_pos );
			$lists['ea_hotfp_pos'] = mosHTML::selectList( $hp, 'ea_hotfp_pos', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_hotfp_pos );
			
            $lists['ea_hot_color'] = $eaconf->ea_hot_color;	
			
			$pp[] = mosHTML::makeOption( '5', '5' );
            $pp[] = mosHTML::makeOption( '10', '10' );
    		$pp[] = mosHTML::makeOption( '25', '25' );
            $pp[] = mosHTML::makeOption( '50', '50' );
            $pp[] = mosHTML::makeOption( '80', '80' );
        	$lists['ea_perpage'] = mosHTML::selectList( $pp, 'ea_perpage', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_perpage );
			
			$lists['ea_fulllocation'] = mosHTML::selectList( $yesno, 'ea_fulllocation', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_fulllocation);

			$s[] = array( "key" => '0', "text" => _EAB_CFG_SHORTINFO1 );
			$s[] = array( "key" => '1', "text" => _EAB_CFG_SHORTINFO2);
			$s[] = array( "key" => '2', "text" => _EAB_CFG_SHORTINFO3);
			$s[] = array( "key" => '3', "text" => _EAB_CFG_SHORTINFO4);
			$s[] = array( "key" => '4', "text" => _EAB_CFG_SHORTINFO5);
			$sinfo = explode(",",$eaconf->ea_shortinfo);
			$lists['ea_shortinfo'] =$eaconf->ea_shortinfo;		
			
        	$lists['ea_mtgcalc'] = mosHTML::selectList( $yesno, 'ea_mtgcalc', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_mtgcalc );
			$lists['ea_mtg_default_interest_percent'] 	= $eaconf->ea_mtg_default_interest_percent;
			$lists['ea_mtg_default_year_term'] =  $eaconf->ea_mtg_default_year_term;
			$lists['ea_mtg_default_down_percent'] =  $eaconf->ea_mtg_default_down_percent;
			$lists['ea_mtg_default_show_progress'] = mosHTML::selectList( $yesno, 'ea_mtg_default_show_progress', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_mtg_default_show_progress );

			$mtf[] = mosHTML::makeOption( '0', _EAB_CFG_STS01 );
			$mtf[] = mosHTML::makeOption( '1', _EAB_CFG_MTF_TYPE1  );
			$mtf[] = mosHTML::makeOption( '2', _EAB_CFG_MTF_TYPE2  );
			$lists['ea_mtf'] = mosHTML::selectList( $mtf, 'ea_mtf', 'class="inputbox"', 'value', 'text', $eaconf->ea_mtf ); 
			$lists['ea_print']  = mosHTML::selectList( $mtf, 'ea_print', 'class="inputbox"', 'value', 'text', $eaconf->ea_print ); 
			
			$lists['ea_f_srch'] = mosHTML::selectList( $yesno, 'ea_f_srch', 'class="inputbox"', 'value', 'text', $eaconf->ea_f_srch ); 			
			$lists['ea_l_srch'] = mosHTML::selectList( $yesno, 'ea_l_srch', 'class="inputbox"', 'value', 'text', $eaconf->ea_l_srch ); 			
			$lists['ea_d_srch'] = mosHTML::selectList( $yesno, 'ea_d_srch', 'class="inputbox"', 'value', 'text', $eaconf->ea_d_srch ); 			

			$lt[] = mosHTML::makeOption( '0', _EAB_CFG_LOC_SRCHTYPE1 );
			$lt[] = mosHTML::makeOption( '1', _EAB_CFG_LOC_SRCHTYPE2 );
			$lists['ea_loc_srchtype'] = mosHTML::selectList( $lt, 'ea_loc_srchtype', 'class="inputbox"', 'value', 'text', $eaconf->ea_loc_srchtype ); 			
									
        	$lists['ea_map24'] = mosHTML::selectList( $yesno, 'ea_map24', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_map24 );
			$mpt[] = mosHTML::makeOption( 'CGI', _EAB_CFG_MAP24_STATIC );
            $mpt[] = mosHTML::makeOption( 'JAVA', _EAB_CFG_MAP24_DYNAMIC );
        	$lists['ea_map24_type'] = mosHTML::selectList( $mpt, 'ea_map24_type', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_map24_type );
			$map = map24_lg_select_list();
			$lists['ea_map24_dlg'] = mosHTML::selectList( $map, 'ea_map24_dlg', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_map24_dlg );			
			$lists['ea_map24_lid'] = $eaconf->ea_map24_lid;
			$lists['ea_map24_dinfo'] = $eaconf->ea_map24_dinfo;
			$lists['ea_map24_tlogo'] = $eaconf->ea_map24_tlogo;
			$lists['ea_map24_dimg'] = $eaconf->ea_map24_dimg;
						
			$lists['ea_allowsubmission'] = mosHTML::selectList( $yesno, 'ea_allowsubmission', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_allowsubmission );
			
			$f[] = mosHTML::makeOption( '0', _EAB_CFG_STS01 );
			$f[] = mosHTML::makeOption( '1', _EAB_CFG_FOOTER1 );
			$f[] = mosHTML::makeOption( '2', _EAB_CFG_FOOTER2 );
			$lists['ea_footer'] = mosHTML::selectList( $f, 'ea_footer', 'class="inputbox"', 'value', 'text', $eaconf->ea_footer );
			
			$frontaccess[] = mosHTML::makeOption( '0', "All" );
			$frontaccess[] = mosHTML::makeOption( '1', "Registered and Above" );
			$frontaccess[] = mosHTML::makeOption( '2', "Author and Above" );
			$frontaccess[] = mosHTML::makeOption( '3', "Editor and Above" );
			$frontaccess[] = mosHTML::makeOption( '4', "Publisher and Above" );
			$frontaccess[] = mosHTML::makeOption( '5', "Manager and Above" );			
			$frontaccess[] = mosHTML::makeOption( '6', "Administrator and Above" );						
			$frontaccess[] = mosHTML::makeOption( '7', "Super Administrator" );
			
			$lists['frontaccess']=mosHTML::selectList( $frontaccess, 'ea_frontaccess', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_frontaccess );
			$frontSuper[] = mosHTML::makeOption( '4', "Publisher and Above" );
			$frontSuper[] = mosHTML::makeOption( '5', "Manager and Above" );			
			$frontSuper[] = mosHTML::makeOption( '6', "Administrator and Above" );						
			$frontSuper[] = mosHTML::makeOption( '7', "Super Administrator" );
			$lists['frontsuper']=mosHTML::selectList( $frontSuper, 'ea_frontsuper', 'class="inputbox" size="1"', 'value', 'text', $eaconf->ea_frontsuper );
			
			$lists['allowpublish'] = mosHTML::selectList( $yesno, 'ea_allowpublish', 'class="inputbox"', 'value', 'text', $eaconf->ea_allowpublish ); 	
			$lists['mail']= mosHTML::selectList( $yesno, 'ea_mail', 'class="inputbox"', 'value', 'text', $eaconf->ea_mail ); 	


			$lists['maxobjects'] =  $eaconf->ea_maxobjects;
			$lists['ea_rss'] =$eaconf->ea_rss; 
			
			//======Google Map========
			$lists['googlemap_enable']= mosHTML::selectList( $yesno, 'ea_googlemap_enable', 'class="inputbox"', 'value', 'text', $eaconf->ea_googlemap_enable ); 	
			
			HTML_estateagent::configEA($lists);
				
		break;
	}
	
}

function galleryRepair($option, $task) {
	global $database,$mosConfig_absolute_path,$eaconf;
	switch($task) {
		case "fixgifpng":
			//=== Gallery images file type!:
			$q = "SELECT `title` , `fname` , `type` , `path` FROM #__estateagent_images WHERE  (`type` = '.gif' OR type = '.png')";
			$database->setQuery($q);
			$res	= $database->loadObjectList();
			foreach($res as $img){
				@rename($mosConfig_absolute_path.$img->path.$img->fname.$img->type,$mosConfig_absolute_path.$img->path.$img->fname.".jpg");
				@rename($mosConfig_absolute_path.$img->path."t".$img->fname.$img->type,$mosConfig_absolute_path.$img->path."t".$img->fname.".jpg");

			}
			//Fix the table now...
			$q='UPDATE #__estateagent_images SET `type` = \'.jpg\' WHERE  (`type` = \'.gif\' OR type = \'.png\')';
			$database->setQuery($q);
			$database->query();
			mosRedirect( "index2.php?option=$option&act=configEA", "Success!" );
			break;
		case "cleardb":
			$q = "SELECT `id`, `title` , `fname` , `type` , `path` FROM #__estateagent_images";
			$database->setQuery($q);
			$res	= $database->loadObjectList();
			foreach($res as $img){
				if(!file_exists($mosConfig_absolute_path.$img->path.$img->fname.$img->type)){
					$obj = new EAImage($database);
					$obj->load($img->id);
					$obj->delete();
				}
			}
			mosRedirect( "index2.php?option=$option&act=configEA", "Success!" );		
			break;
		case "clearfiles":
			$p=$mosConfig_absolute_path .$eaconf->ea_picpath;
			$imgFiles 	= mosReadDirectory(  $p);
			foreach ($imgFiles as $file) {
				$i_f 	= $p .'/'. $file;

					if ( preg_match("/t?(ea.+)\.(bmp|jpg|png|gif)/",$file,$match )) {
						$q= "SELECT count(id) FROM #__estateagent_images WHERE `fname` = '$match[1]' AND objid!=0";
						$database->setQuery($q);
						$c=$database->loadResult();
						echo "|".$c."|";
						if($c==0){
							unlink($i_f);
						}
						
					}
				}
			$q = "SELECT `id` FROM #__estateagent_images WHERE objid=0";
			$database->setQuery($q);
			$res	= $database->loadObjectList();
			foreach($res as $img){
					$obj = new EAImage($database);
					$obj->load($img->id);
					$obj->delete();
			}
			mosRedirect( "index2.php?option=$option&act=configEA", "Success!" );		
			break;
		case "fixthumbs":
			require($mosConfig_absolute_path."/administrator/components/com_estateagent/includes/gallery/resize.gallery.php");
			$q = "SELECT id FROM #__estateagent_images  WHERE  `type` = '.jpg' ";
			echo $q;
			$database->setQuery($q);
			$ids=$database->loadResultArray();

			foreach($ids as $id){
					$obj = new EAImage($database);
					$obj->load($id);
					if(!file_exists($mosConfig_absolute_path.$obj->path."t".$obj->fname.$obj->type)){
						$src_file=$mosConfig_absolute_path.$obj->path.$obj->fname.$obj->type;
						$dest_thmb=$mosConfig_absolute_path.$obj->path."t".$obj->fname.$obj->type;
						$t= resizeIMG(1,$src_file,$dest_thmb,$eaconf->ea_tbwidth,$eaconf->ea_tbheight,0,$eaconf->ea_tbqulty);
					}
			
			
			}
			mosRedirect( "index2.php?option=$option&act=configEA", "Success!" );		
			break;
		default:
			mosRedirect( "index2.php?option=$option&act=configEA");
			break;
			
	
	}	
}

?>